﻿using MediatR;
using S.Parturition.API.Application.Command;
using S.Parturition.Domain;

namespace S.Parturition.API.Application.CommandHandlers
{
    public class UpdateLambStateHandler : IRequestHandler<UpdateLambStateCommand, bool>
    {

        private readonly MyDbContext dbContext;

        public UpdateLambStateHandler(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public async Task<bool> Handle(UpdateLambStateCommand request, CancellationToken cancellationToken)
        {
            //获取到对应耳号的信息
            var lamb = dbContext.LambRegistrationModel.Where(x => x.EarTag == request.EarTag).OrderByDescending(x => x.CreateDate).FirstOrDefault();
            //将耳号的状态改为不可使用
            lamb.SheepStatus = lamb.SheepStatus == true ? false : true;
            //修改
            dbContext.Update(lamb);
            return dbContext.SaveChanges() > 0;

        }
    }
}
